package com.vf.cloud.paas.app.master.api.scene.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.jfinal.kit.StrKit;
import com.vf.cloud.paas.app.master.api.scene.service.ISceneLayer;
import com.vf.cloud.paas.app.master.common.domain.SceneActor;
import com.vf.cloud.paas.app.master.common.domain.SceneLayer;
import com.vf.cloud.paas.app.master.common.domain.User;
import com.vf.cloud.paas.app.master.common.vo.scene.SceneActorPageP;
import com.vf.cloud.paas.app.master.common.vo.scene.SceneTree;
import com.vf.cloud.paas.app.master.common.vo.scene.SceneTreeParam;
import com.vf.cloud.paas.common.util.R;
import com.vf.cloud.paas.common.util.UuidUtil;
import com.vf.cloud.paas.common.util.WebUtils;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/scene/actor")
public class SceneActorController {

	@Autowired
	private ISceneLayer sceneLayerImpl;

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public R<List<SceneActor>> list(@RequestBody SceneActorPageP param) throws Exception {

		String sqlExceptSelect = " FROM " + SceneActor.TABLE_NAME + " o  WHERE 1=1 ";
		if (!StrKit.isBlank(param.getKeywords())) {
			sqlExceptSelect += " AND ( O.NAME LIKE '%" + param.getKeywords() + "%' ) ";
		}
		sqlExceptSelect += "  ORDER BY   O.MODIFY_TIME desc ";
		List<SceneActor> sceneActors = SceneActor.dao.find("select *  " + sqlExceptSelect);
		return R.ok(sceneActors);
	}

	@RequestMapping(value = "/addLayer", method = RequestMethod.POST)
	public R<List<SceneTree>> addLayer(@RequestBody SceneTreeParam param) {

		SceneLayer sceneLayer = new SceneLayer();
		sceneLayer.setId(UuidUtil.getUUID());
		sceneLayer.setLabel("新增文件夹");
		sceneLayer.setPid(StrKit.isBlank(param.getPid()) ? "-1" : param.getPid());
		sceneLayer.save();
		return R.ok(sceneLayerImpl.getTree(param));
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public R<SceneActor> save(HttpServletRequest request,@RequestBody SceneActor param) {
		User user = WebUtils.getCurUser(request);
		if(StrKit.isBlank(param.getId())) {
			param.setId(UuidUtil.getUUID());
			param.setCreateUserId(user.getId());
			if(param.save()) {
				 return R.ok(param);
			}
		}
		param.setModifyUserId(user.getId());
		if (param.update()) {
			return R.ok(param);
		}
		return R.failed();
	}

	@RequestMapping(value = "/getById", method = RequestMethod.POST)
	public R<SceneActor> getById(@RequestBody SceneActor param) {
		if (param == null) {
			return R.failed("参数必填.");
		}
		SceneActor sceneActor = SceneActor.dao.findById(param.getId());
		if (sceneActor == null) {
			return R.failed("无效参数.");
		}
		return R.ok(sceneActor);
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public R<SceneActor> delete(@RequestBody SceneActor param) {
		if (param == null) {
			return R.failed("参数必填.");
		}

		SceneActor sceneActor = SceneActor.dao.findById(param.getId());
		if (sceneActor == null) {
			return R.failed("无效参数.");
		}
		if (!sceneActor.delete()) {
			return R.failed("删除失败.");
		}
		return R.ok(sceneActor);
	}
}
